package com.yudaob2b.elong.task;

import com.yudaob2b.elong.entity.dto.ELaElongHotelRpDTO;
import com.yudaob2b.elong.entity.vo.HotelRatePlan;
import com.yudaob2b.elong.enums.ela.EnumElasticElongIndexName;
import com.yudaob2b.elong.service.HotelRpService;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 产品详情入库线程
 *
 * @author Kidd.Tan
 * @email 298023780@qq.com
 * @date 7/20/18 8:42 PM
 */
public class DataRpLocalDbTask implements Runnable, Serializable {
    private static Logger dataRpLocalDbTaskLogger = LoggerFactory.getLogger("dataRpLocalDbTaskLogger");

    private List<HotelRatePlan> rpList;
    private HotelRpService hotelRpService;
    private ClientInterface clientUtil;

    public DataRpLocalDbTask(List<HotelRatePlan> rpList, HotelRpService hotelRpService
            , ClientInterface clientUtil) {
        this.rpList = rpList;
        this.hotelRpService = hotelRpService;
        this.clientUtil = clientUtil;
    }

    @Override
    public void run() {
        // 保存搜索引擎
        saveEla();
    }

    /**
     * 保存搜索引擎
     */
    private void saveEla() {
        try {
            // 待插入列表
            List<ELaElongHotelRpDTO> newList = new ArrayList();
            for (HotelRatePlan rp : rpList) {
                ELaElongHotelRpDTO dto = hotelRpService.getElaDtoByRp(rp);
                newList.add(dto);

            }
            if (newList.size() > 0) {
                clientUtil.addDocuments(EnumElasticElongIndexName.RP.getIndexName(),//索引表
                        EnumElasticElongIndexName.RP.getType(),//索引类型
                        newList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //dataRateLocalDbTaskLogger.info("---产品详情入库线程总耗时：-insert:{},--{}", newList.size(),  System.currentTimeMillis() - start);
    }

}
